#region Summary

// 
// Owner : JackieHan 
// 
// Solution : LearnSolution 
// 
// Project : JackieHan.NHibernate3.Configuration
// 
// File : FluentConfiguration.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 流配置
// 
// History : 
//			2010-09-09 23:27:17	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using NHibernate;
using NHibernate.AdoNet;
using NHibernate.ByteCode.Castle;
using NHibernate.Cfg;
using NHibernate.Cfg.Loquacious;
using NHibernate.Cache;
using NHibernate.Connection;
using NHibernate.Exceptions;
using NHibernate.Transaction;
using NHibernate.Type;
using NHibernate.Driver;

#endregion

namespace JackieHan.NHibernate3.Repository
{
    /// <summary> 
    /// 流配置
    /// </summary>
    public class FluentConfiguration
    {
        public static Configuration CompleteConfiguration()
        {
            Configuration cfg = new Configuration();
            cfg.SessionFactory()
                .Named("NHibernate")  //配置助记名
                .GenerateStatistics()     //开启收集有助于性能调试的统计数据
                .Using(EntityMode.Poco)//实体处理方式
                .ParsingHqlThrough<NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory>() //Hql翻译提供程序
                .Caching    //缓存设置
                    .PrefixingRegionsWith("xyz") //前缀
                    .Queries//查询
                        .Through<StandardQueryCache>()  //查询性能
                    .Through<HashtableCacheProvider>()  //缓存类型
                    .UsingMinimalPuts()         ////设置开启以频繁的读操作为代价, 优化二级缓存来最小化写操作. 
                    .WithDefaultExpiration(15)  //设置超时时间
                .Mapping  //配置设置
                    .UsingDefaultCatalog("learnCatlog")   //默认类别
                    .UsingDefaultSchema("learnSchema") //默认数据架构
                .GeneratingCollections   //集合设置
                    .Through<DefaultCollectionTypeFactory>()  ////集合提供类名称
                .Proxy  //延迟加载设置
                    .DisableValidation()  ////关闭验证    加载实体映射元数据之后，对需要代理的实体使用IProxyFactoryFactory的ProxyValidator进行验证（例如要求实体有默认构造器、方法属性必须为virtual类型等）
                    .Through<ProxyFactoryFactory>() //延迟加载提供类
                .Integrate  //数据库相关设置
                    .AutoQuoteKeywords()
                    .BatchingQueries
                        .Through<SqlClientBatchingBatcherFactory>()
                        .Each(15)
                    .Connected
                        .By<SqlClientDriver>()
                        .Releasing(ConnectionReleaseMode.AfterTransaction)
                        .Through<DriverConnectionProvider>()
                        .With(IsolationLevel.ReadCommitted)
                        .ByAppConfing("learn")
                    .CreateCommands
                        .AutoCommentingSql()
                        .ConvertingExceptionsThrough<SQLStateConverter>()
                        .Preparing()
                        .WithMaximumDepthOfOuterJoinFetching(13)
                        .WithTimeout(30)
                        .WithDefaultHqlToSqlSubstitutions()
                    .DisableKeywordsAutoImport()
                    .DisableLogFormatedSql()
                    .LogSqlInConsole()
                    .Schema
                        .Creating()
                    .Transactions
                        .Through<AdoNetTransactionFactory>();
            return cfg;
        }
    }
}